package vista;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;

/**
 * this class will create the primary aspects for the visualization of the user
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class GraphicPlayer {
	
	public Rectangle bounds;
	private Image playerImage;
	public Point moveDir = new Point();
	public Point destiny;
	public int speed = 2;
	
	/**
	 * this will construct a new user
	 * @param bounds the size of the zone that you want to get 
	 * @param image the user image 
	 */
	public GraphicPlayer(Rectangle bounds, Image image) {
		this.bounds = bounds;
		this.destiny= new Point(bounds.x,bounds.y);
		this.playerImage = image;
		
	} 
	/**
	 * this will move our avatar to the next position in the board
	 * @param destiny the point in the main board to move the player
	 */
	public void setPlayerMove(Point destiny) {
		this.destiny.x = destiny.x - bounds.width/2;
		this.destiny.y = destiny.y - bounds.height/2;
		moveDir.x = destiny.x - bounds.x;
		moveDir.y = destiny.y - bounds.y;
		
		double module = Math.sqrt(moveDir.x*moveDir.x + moveDir.y*moveDir.y);
		
		moveDir.x = Math.round((float)(moveDir.x / module));
		moveDir.y = Math.round((float)(moveDir.y / module));
	}
	
	/**
	 * this will calculate a relative pos to move the player 
	 */
	private void calculatePos() {
		if(bounds.x < destiny.x) {
			if(moveDir.x == 0) bounds.x += 1;
			else bounds.x += moveDir.x;
			
		}
		if(bounds.x > destiny.x) {
			if(moveDir.x == 0) bounds.x -= 1;
			else bounds.x += moveDir.x;
		}
		if(bounds.y < destiny.y) {
			if(moveDir.y == 0) bounds.y += 1;
			else bounds.y += moveDir.y;
		}
		if(bounds.y > destiny.y) {
			if(moveDir.y == 0) bounds.y -= 1;
			else bounds.y += moveDir.y;
		}
	}
	/**
	 * this method will make a visual move in the board
	 * @param g the graphics to draw (Player Image) 
	 */
	public void render(Graphics g) {
		calculatePos();
		g.drawImage(playerImage, bounds.x, bounds.y, bounds.width, bounds.height, null);
	}

}
